import {observable, action, runInAction} from 'mobx';
import {WeaTools} from 'ecCom';
import * as urls from '../constants/ServerUrls';

const {callApi} = WeaTools;

export default class MyFavouriteStore {
    @observable allDirName = '';
    @observable addDirTitle = '';
    @observable addBtnName = '';
    @observable addBtnList = [];
    @observable typeList = [];
    @observable moveBtnName = '';
    @observable deleteBtnName = '';
    @observable repeatIconTitle = '';
    @observable editIconTitle = '';
    @observable moveIconTitle = '';
    @observable deleteIconTitle = '';
    @observable loadingmsg = '';
    @observable nodata = '';
    @observable nomoredata = '';
    @observable favTypeMap = {};

    /**
     * SysFavouriteStore的实例
     */
    sysFavouriteStore = null;

    constructor(sysFavouriteStore) {
        this.sysFavouriteStore = sysFavouriteStore;
    }

    /**
     * 初始化收藏页面
     */
    @action initPage() {
        const initurl = urls.PAGE_INIT_URL;
        callApi(initurl, 'POST', {}).then((resdata) => {
            this.init(resdata);

            const typeList = [...resdata.typeList];
            let value = '';
            for (let i = 0; i < typeList.length; i++) {
                const typeMap = typeList[i];
                const {selected, id} = typeMap;
                if (selected) {
                    value = id;
                    break;
                }
            }

            /**
             * 设置收藏类型默认选中的下拉选项
             */
            this.sysFavouriteStore.setFavouriteType(value);
        });
    }

    @action.bound
    init = (data) => {
        runInAction(() => {
            this.allDirName = data.allDirName;
            this.addDirTitle = data.addDirTitle;
            this.addBtnName = data.addBtnName;
            this.addBtnList = data.addBtnList;
            this.typeList = data.typeList;
            this.moveBtnName = data.moveBtnName;
            this.deleteBtnName = data.deleteBtnName;
            this.repeatIconTitle = data.repeatIconTitle;
            this.editIconTitle = data.editIconTitle;
            this.moveIconTitle = data.moveIconTitle;
            this.deleteIconTitle = data.deleteIconTitle;
            this.loadingmsg = data.loadingmsg;
            this.nodata = data.nodata;
            this.nomoredata = data.nomoredata;
            this.favTypeMap = data.favTypeMap;
        })
    }
}
